package main.objects;

import main.FactageSmet;
import main.MainFrame;
import main.MsSQLConnect;

import javax.swing.tree.DefaultMutableTreeNode;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;


public class TreeTitulNode extends DefaultMutableTreeNode{

    private String name, shortName, code;
    private int id, parentId, titulId;
    public boolean isTitul, isEPS;
    private ArrayList<SmetNode> smets;
    private HashMap<String, SmetResource> titulResources;

    //region Конструктор для рута
    public TreeTitulNode(String name, int id) {

        this.name = name;
        this.id = id;
        this.isTitul = false;
        this.isEPS = false;
        setUserObject(id + "-root");

    }
    //endregion

    //region Конструктор для титула
    public TreeTitulNode(String name, int id, String code) {

        this.name = name;
        this.id = id;
        this.code = code;
        this.isTitul = true;
        this.isEPS = false;
        setUserObject(id + "-titul");
        this.titulResources = new HashMap<>();

    }
    //endregion

    //region Конструктор для EPS
    public TreeTitulNode(String name, int id, int parentId, int titulId, String code) {

        this.name = name;
        this.id = id;
        this.parentId = parentId;
        this.titulId = titulId;
        this.code = code;
        this.isTitul = false;
        this.isEPS = true;
        setUserObject(id + "-eps");
        smets = new ArrayList<>();

    }

     public TreeTitulNode(ResultSet rs, String type) throws SQLException {

         if (type.equals("EPS")) {

             this.id = rs.getInt("id");
             this.name = rs.getString("name");
             this.parentId = rs.getInt("parent_id");
             this.titulId = rs.getInt("titul_id");
             this.code = rs.getString("code");
             this.isTitul = false;
             this.isEPS = true;
             setUserObject(id + "-eps");
             smets = new ArrayList<>();

         }

    }

    //endregion

    public void updateName(String newName) {

        this.name = newName;


        try {
            FactageSmet.IMPORT_FACTAGE_CONNECTION = MsSQLConnect.ConnectDB("ImportFactage");

            if (isTitul) {

                Statement stmt = FactageSmet.IMPORT_FACTAGE_CONNECTION.createStatement();
                stmt.executeUpdate("UPDATE Titul SET name = '"+newName+"' WHERE id = " + id);

                stmt.close();

                MainFrame.ALL_EPS.put(getUserObject() + "-titul", this);

            } else {

                Statement stmt = FactageSmet.IMPORT_FACTAGE_CONNECTION.createStatement();
                stmt.executeUpdate("UPDATE EPS SET name = '"+newName+"' WHERE id = " + id);

                stmt.close();

                MainFrame.ALL_EPS.put(getUserObject() + "-eps", this);

            }

            FactageSmet.IMPORT_FACTAGE_CONNECTION.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

    public int getTitulId() {
        return titulId;
    }

    public void setTitulId(int titulId) {
        this.titulId = titulId;
    }

    public String toString() {
        return name;
    }

    public void setTitul(boolean isTitul) {
        this.isTitul = isTitul;
    }

    public ArrayList<SmetNode> getSmets() {
        return smets;
    }

    public HashMap<String, SmetResource> getTitulResources() {
        return titulResources;
    }

}
